<?php

class SupplierDeliveryReturnsController extends AppController {

    var $name = 'SupplierDeliveryReturns';
    var $pageTitle = 'Delivery Request Return';
    var $viewPath = 'suppliers/returns/delivery';
    var $uses = array('DeRtH', 'DeRH', 'DeR', 'Customer', 'Site', 'RunningNo', 'CustProductSite', 'DeRt', 'DeRtStatus');
    var $paginate = array(
        'DeRtH' => array(
            'order' => 'DeRtH.id DESC'
        )
    );

    function beforeFilter() {
        parent::beforeFilter();
        $this->set('parentNav', 'main_supplier_returns');
    }

    function display() {
        parent::display('DeRtH');
        $this->paginate['DeRtH']['contain'] = array('Supplier', 'DeRtStatus', 'DeRH', 'Customer', 'Site');

        $this->_checkSupplier('DeRtH');
        $this->_checkBetweenSearch('DeRtH', 'return_date');
        $this->_paging('DeRtH');
    }

    /**
     * Accept return
     * ปรับของที่ return เข้าคลัง
     */
    function save() {
        $is_success = true;

        $this->DeRtH->query("BEGIN");
        $id = $this->data['DeRtH']['id'];
        $status = $this->data['DeRtH']['delivery_return_status_id'];

        if ($status == DeRtStatus::RETURNED) {
            // ปรับ status ว่า return แล้ว
            $this->DeRtH->id = $id;
            $is_success &= $this->DeRtH->saveField('delivery_return_status_id', DeRtStatus::RETURNED);
            // ปรับของเข้า Stock
            $deRs = $this->DeRt->findAllByDeliveryReturnHeadId($id);
            foreach ($deRs as $data) {
                $this->CustProductSite->id = $data['DeRt']['cust_product_site_id'];

                // เพิ่ม supplier_current_stock ตามจำนวนที่ return
                $supplier_cs = $this->CustProductSite->field('supplier_current_stock');
                $is_success &= $this->CustProductSite->saveField('supplier_current_stock', $supplier_cs + $data['DeRt']['return_qty']);
            }
        } elseif ($status == DeRtStatus::DECLINED) {
            $this->DeRtH->id = $id;
            $is_success &= $this->DeRtH->saveField('delivery_return_status_id', DeRtStatus::DECLINED);
        }

        if ($is_success) {
            $this->DeRtH->query("COMMIT");
            $this->Session->setFlash("Save succesfull.", 'flash/ok');
        } else {
            $this->DeRtH->query("ROLLBACK");
            $this->Session->setFlash("Save fail !", 'flash/fail');
        }
        $this->setAction('view', $id);
    }

    function view($id = null) {
        // mode edit
        if ($id != null) {
            $this->DeRtH->contain(array('Customer', 'Site', 'DeRH', 'Supplier', 'DeRtStatus',
                'DeRt' => array('Product' => array('Unit'), 'CustProductSite')));
            $this->data = $this->DeRtH->findById($id);

            $this->_neighbors($id, $this->DeRtH, array('fields' => array('id', 'return_no')));
            $this->set('mode', 'edit');
        } elseif (isset($this->passedArgs['delivery_request_head_id'])) {
            // มาจากหน้า Delivery Request
            $this->DeRtH->contain(array('Customer', 'Site', 'DeRH', 'Supplier', 'DeRtStatus',
                'DeRt' => array('Product' => array('Unit'), 'CustProductSite')));
            $this->data = $this->DeRtH->findByDeliveryRequestHeadId($this->passedArgs['delivery_request_head_id']);

            $this->_neighbors($id, $this->DeRtH, array('fields' => array('id', 'return_no')));
            $this->set('mode', 'edit');
        }
        $this->set('statuses', $this->DeRtStatus->find('list'));
    }

    function print_view($id) {
        $this->layout = 'print';
        $this->DeRtH->contain(array('Supplier', 'Customer', 'Site', 'DeRtStatus', 'DeRt' => array('Product' => array('Unit'), 'CustProductSite')));
        $this->data = $this->DeRtH->findById($id);
    }

}

?>